#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/ide.h>
#include <linux/delay.h>

#define LED_MAJOR 200   /* 主设备号 */
#define LED_NAME  "LED" /* 设备名字 */

#define LEDOFF 0 /* 关灯 */
#define LEDON  1 /* 开灯 */

#define CCM_CCGR1_BASE                (0x020C406C)
#define IOMUXC_SW_MUX_GPIO1_IO03_BASE (0x020E0068)
#define IOMUXC_SW_PAD_GPIO1_IO03_BASE (0x020E02F4)
#define GPIO1_DR_BASE                 (0x0209C000)
#define GPIO1_GDIR_BASE               (0x0209C004)


/* 映射后的寄存器虚拟地址指针 */
static void __iomem *CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;




static int led_open(struct inode *inode, struct file *filp)
{

    return 0;
}


static ssize_t led_read(struct file *filp,char __user *buf,size_t cnt, loff_t *offt)
{

    return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue;
    u32 value;
    unsigned char databuf[1];
    unsigned char cmd;
    
    retvalue = copy_from_user(databuf,buf,cnt);
    if(retvalue < 0){
        printk("kernel write failed! \r\n");
        return -1;
    }
    cmd = databuf[0];
    if(cmd == LEDON){
        value = readl(GPIO1_DR);
        value &= ~(1 <<3); /* 亮 */
        writel(value,GPIO1_DR); 
    }

    if (cmd == LEDOFF){
    
        value = readl(GPIO1_DR);
        value |= (1 <<3); /* 灭 */
        writel(value,GPIO1_DR);
    }
    return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
    return 0;
}

static struct file_operations led_fops = 
{
    .owner = THIS_MODULE, 
    .open = led_open,
    .read = led_read,
    .write = led_write,
    .release = led_release,
};

/* 驱动入口函数 
 * 初始化LED
 */
static int __init led_init(void)
{
    int retvalue = 0;
    u32 val;
    printk("led start initing!!! \r\n");

    /* 地址映射 */
    CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
    SW_MUX_GPIO1_IO03 = ioremap(IOMUXC_SW_MUX_GPIO1_IO03_BASE, 4);
    SW_PAD_GPIO1_IO03 = ioremap(IOMUXC_SW_PAD_GPIO1_IO03_BASE, 4);
    GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
    GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);

    /* 配置GPIO1相关的寄存器 */
    val = readl(CCM_CCGR1);
    val &= ~(0x03<<26);
    val |= (0x03<<26);
    writel(val, CCM_CCGR1);

    writel(5,SW_MUX_GPIO1_IO03);
    writel(0x10B0,SW_PAD_GPIO1_IO03);

    val = readl(GPIO1_GDIR);
    val &= ~(1<<3);
    val |= (1<<3);
    writel(val,GPIO1_GDIR);   

    val = readl(GPIO1_DR);
    val |= (1<<3);   /* 默认关闭 */
    writel(val,GPIO1_DR);  


    /* 注册字符设备驱动 */
    retvalue = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
    if(retvalue < 0){
        printk("register_chrdev failed!\r\n");
        return -1;
    }
    return 0;
}

/* 驱动出口函数 */
static void __exit led_exit(void)
{
    /* 取消映射 */
    iounmap(CCM_CCGR1);
    iounmap(SW_MUX_GPIO1_IO03);
    iounmap(SW_PAD_GPIO1_IO03);
    iounmap(GPIO1_DR);
    iounmap(GPIO1_GDIR);
    /*  */
    printk("led_exit!!! \r\n");
    unregister_chrdev(LED_MAJOR,LED_NAME);
}


module_init(led_init);
module_exit(led_exit);




MODULE_LICENSE("GPL");
MODULE_AUTHOR("chaoliang");





